package team.yidong.fenda.wx.controller;

import com.qiniu.util.Json;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import team.yidong.fenda.entity.Hospital;
import team.yidong.fenda.entity.Question;
import team.yidong.fenda.entity.User;
import team.yidong.fenda.service.MasterService;
import team.yidong.fenda.wx.Service.WxHospitalFieldService;
import team.yidong.fenda.wx.Service.WxHospitalService;
import team.yidong.fenda.wx.Service.WxMasterService;


import java.util.*;

/**
 * Created by xuanlong on 2017/10/10.
 */
@Controller
public class WxHospitalController {
    @Autowired
    WxHospitalService wxHospitalService;
    @Autowired
    WxHospitalFieldService wxHospitalFieldService;
    @Autowired
    MasterService masterService;

    /**
     * 返回首页的轮播图和分类
     *
     * @param id
     * @return
     */
    @RequestMapping("/wx/findHospital")
    @ResponseBody
    public JSONObject findWxHopital(@RequestParam Integer id) {

        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(new String[]{"hospitalPermissionSet", "hospital", "userSet", "masterList"});
        return JSONObject.fromObject(wxHospitalService.findWxHospital(id), jsonConfig);
    }

    @RequestMapping("/wx/findHospitalField")
    @ResponseBody
    public JSONArray findHospitalField(@RequestParam Integer hospitalId) {

        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(new String[]{"hospital"});
        return JSONArray.fromObject(wxHospitalFieldService.findHospitalFieldByHospital(hospitalId), jsonConfig);
    }

    @RequestMapping("/wx/findHospitalFieldAndIconByMasterOpenId")
    @ResponseBody
    public JSONArray findHospitalFieldAndIconByMasterOpenId(@RequestParam String login_uuid) {
        Jedis jedis = new Jedis("localhost");
        String openid = jedis.get(login_uuid);
        Integer hospitalId = masterService.findByOpenId(openid).getHospital().getId();
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(new String[]{"hospital"});
        return JSONArray.fromObject(wxHospitalFieldService.findHospitalFieldByHospital(hospitalId), jsonConfig);
    }
}